<template>
  <van-button
    v-if="isFollowed"
    class="follow-btn"
    type="info"
    color="#3296fa"
    :loading="followLoading"
    round
    size="small"
    icon="plus"
    @click="onFollow"
  >关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading="followLoading"
  >已关注</van-button>
</template>

<script>
import { follow, noFollow } from "@/api/article.js";
export default {
  name: "FollowUser",
  data() {
    return {
      followLoading: false
    };
  },
  // 自定义 v-model 的数据名称
  model: {
    prop: "isFollowed", // 默认是 value
    event: "update-is_followed" // 默认是 input
  },
  props: {
    isFollowed: {
      typo: Boolean,
      required: true
    },
    userId: {
      type: [Number, String],
      required: true
    },
  },
  methods: {
    async onFollow() {
      try {
        this.followLoading = true;
        if (this.isFollowed) {
          await follow(this.userId);
          //   this.isFollowed = false;
        } else {
          await noFollow(this.userId);
          //   this.isFollowed = true;
          // console.log(data);
        }
        this.$emit("update-is_followed", !this.isFollowed);
        this.$toast.success(this.isFollowed ? "关注成功" : "取消关注");
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail("你不能关注自己");
        }
        this.$toast.fail("操作失败");
      }
      this.followLoading = false;
    }
  }
};
</script>

<style lang="scss" scoped>
</style>